package pizza.admin.entity;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Date;
import java.util.Formatter;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.OneToMany;
import javax.persistence.PostPersist;
import javax.persistence.Transient;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import pizza.admin.dao.UserDAO;
import pizza.admin.helper.HtmlHelper;

@Entity
@Table(name = "User")
public class User implements java.io.Serializable {
	@Transient
	public static String SALT = "daposkpf234234no@$#@$&(@#erdewfe";
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = true)
	private Integer id;
	
	@Column(name = "name", unique = false, nullable = false)
	private String name;
	
	@Column(name = "surname", unique = false, nullable = false)
	private String surname;
	
	@Column(name = "username", unique = false, nullable = false)
	private String username;
	
	@Column(name = "password", unique = false, nullable = false)
	private String password;
	
	@Transient
	private String passwordRepeat;
	
	@Column(name = "isAdmin", unique = false, nullable = false)
	private Boolean isAdmin;
	
    @OneToMany(cascade= CascadeType.REMOVE,mappedBy="user", fetch = FetchType.LAZY)
    private Collection<Order> orders;
    
	public User() {
		id = null;
		isAdmin = false;
		username = "";
		password = "";
		passwordRepeat = "";
		name = "";
		surname = "";
		
	}


	public Integer getId() {
		return id;
	}


	public void setId(Integer id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getSurname() {
		return surname;
	}


	public Boolean getIsAdmin() {
		return isAdmin;
	}


	public void setIsAdmin(Boolean isAdmin) {
		this.isAdmin = isAdmin;
	}


	public void setSurname(String surname) {
		this.surname = surname;
	}


	public String getUsername() {
		return username;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordRepeat() {
		return passwordRepeat;
	}


	public void setPasswordRepeat(String passwordRepeat) {
		this.passwordRepeat = passwordRepeat;
	}
	
	@PostPersist
	public void encodePasswordForDB(){
		//password = this.encryptPassword(password+this.SALT);
		password = encryptPassword(password);
	}
	public static String encryptPassword(String password)
	{
	    String sha1 = "";
	    try
	    {
	        MessageDigest crypt = MessageDigest.getInstance("SHA-1");
	        crypt.reset();
	        crypt.update(password.getBytes("UTF-8"));
	        sha1 = byteToHex(crypt.digest());
	    }
	    catch(NoSuchAlgorithmException e)
	    {
	        e.printStackTrace();
	    }
	    catch(UnsupportedEncodingException e)
	    {
	        e.printStackTrace();
	    }
	    return sha1;
	}

	public static String byteToHex(final byte[] hash)
	{
	    Formatter formatter = new Formatter();
	    for (byte b : hash)
	    {
	        formatter.format("%02x", b);
	    }
	    String result = formatter.toString();
	    formatter.close();
	    return result;
	}
	
	
	public Boolean validateForDB(){
		UserDAO userDAO = new UserDAO();
		if(id==null){
			
//			validate before add
			if(password.isEmpty()
					||!password.equals(passwordRepeat)
					||username.isEmpty()
					||userDAO.checkUniqueUserName(username,id)
					||username.length()<5){
				return false;
			}
			return true;
		}else{
//			validate before edit	
			if(userDAO.checkUniqueUserName(username,id)||username.length()<5){
				return false;
			}
			if(!password.isEmpty()&&!password.equals(passwordRepeat)){
				return false;
			}
			
			return true;
		}
	}
	
	public void purifyFields(){
		HtmlHelper helper = new HtmlHelper();
		name = helper.purify(name);
		surname = helper.purify(surname);
		username = helper.purify(username);
		
	}
}
